//
//  OrderTeacherController.m
//  SDSY
//
//  Created by 蓝布鲁 on 2017/3/22.
//  Copyright © 2017年 蓝布鲁. All rights reserved.
//

#import "OrderTeacherController.h"
#import "DOPDropDownMenu.h"
#import "TeacherCell.h"
#import "TeacherModel.h"
#import "TeacherInfoViewController.h"
#import "MoreSortPickTimeController.h"

@interface OrderTeacherController ()<DOPDropDownMenuDataSource,DOPDropDownMenuDelegate,UITableViewDelegate,UITableViewDataSource>

// 视图
@property (nonatomic,retain)DOPDropDownMenu *menu;
@property (nonatomic,retain)UITableView *tableView;

// 数据
@property (nonatomic,retain)NSArray *collegeArray;
@property (nonatomic,retain)NSArray *teacherTypeArray;
@property (nonatomic,retain)NSArray *sortArray;
@property (nonatomic,retain)NSArray *stateArray;
@property (nonatomic,retain)NSMutableArray *dataArray;
@property (nonatomic,assign)NSInteger page;


@end

@implementation OrderTeacherController

- (instancetype)init {
    if (self=[super init]) {
        self.title = @"预约导师";
        self.view.backgroundColor = [UIColor whiteColor];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"更多查询"
                                                                                  style:UIBarButtonItemStylePlain
                                                                                 target:self
                                                                                 action:@selector(moreSortTapped)];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 1.初始化下拉菜单
    self.menu = [[DOPDropDownMenu alloc] initWithOrigin:CGPointMake(0, 64) andHeight:44];
    self.menu.delegate = self;
    self.menu.dataSource = self;
    
    // 2.初始化tableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.tableView];
    
    // 网络获取下拉数据,先本地获取
    [self updateMenuDate];
    
    // 3.获取网络数据
    
    // 下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 请求导师数据
        self.page = 0;
        [self updateTeacherDateWithPage:self.page getCount:12];
    }];
    
    [self.tableView.mj_header beginRefreshing];
    
    // 上拉加载
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 获取导师数据
        self.page++;
        [self updateTeacherDateWithPage:self.page getCount:12];
    }];
}

#pragma mark - 网络请求

// 网络获取下拉数据
- (void)updateMenuDate {
#pragma mark 获取网络数据
    self.collegeArray =  COLLAGE_NAME;
    self.teacherTypeArray = TEACHER_TYPE;
    self.stateArray = @[@"全部状态",@"可预约", @"不可预约"];
    self.sortArray = @[@"预约最多排前",@"预约最少排前"];
    [self.menu reloadData];
    [self.menu selectDefalutIndexPath];
    
    // 可以添加如下方法,默认选择下拉类型
//    DOPIndexPath *indexpath = [[DOPIndexPath alloc] initWithColumn:1 row:self.activityType];
//    [self.menu selectIndexPath:indexpath];
}

// 请求老师信息
- (void)updateTeacherDateWithPage:(NSInteger)page getCount:(NSInteger)count {
    // 判断是否(下拉刷新)
    if (page == 0) {
        // 下拉刷新,则清空数组
        self.dataArray = [[NSMutableArray alloc] init];
    }
    
    // 初始化请求参数
    NSDictionary *parameters = @{@"teacher_college": @(self.college+1), @"teacher_type": @(self.teacherType+1), @"teacher_sort": @(self.state),@"page": @(page), @"count": @(count)};

    
    [[NetworkTool shareInstance] requireMethodType:POSTType URLString:@"SDSY/getTeacher" parameters:parameters success:^(NSDictionary *respondDictionary) {
        // 1.储存数据
        for (NSDictionary *dic in respondDictionary[@"Teachers"]) {
            TeacherModel *model = [[TeacherModel alloc] initWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        [self.tableView reloadData];
        
        // 2.检查是否正常加载
        if ([respondDictionary[@"Teachers"] count] < count) {
            // 没有更多数据
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else {
            // 正常结束刷新
            [self.tableView.mj_footer endRefreshing];
        }
        [self.tableView.mj_header endRefreshing];
        
    } failure:^(NSError *error) {
        
        [self updateFailed];
    }];
    
}

// 如果请求失败,HUD显示错误,tableVIew刷新空数据,foot提示没有数据
- (void)updateFailed {
    [SVProgressHUD showfailed];
    [self.tableView.mj_footer endRefreshingWithNoMoreData];
    [self.tableView.mj_header endRefreshing];
    [self.tableView reloadData];
}

- (void)moreSortTapped{
    MoreSortPickTimeController *vc = [[MoreSortPickTimeController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    [self presentViewController:nav animated:YES completion:nil];
}

#pragma mark DOPDropDownMenuDataSource回调方法
/**
 *  返回 menu 有多少列 ，默认1列
 */
- (NSInteger)numberOfColumnsInMenu:(DOPDropDownMenu *)menu {
    return 3;
}

/**
 *  返回 menu 第column列有多少行
 */
- (NSInteger)menu:(DOPDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column {
    if (column == 0) {
        // 按学院筛选
        return self.collegeArray.count;
    }else if (column == 1) {
        // 按导师类型筛选
        return self.teacherTypeArray.count;
    } else{
        // 可预约状态
        return self.stateArray.count;
    }
}

/**
 *  返回 menu 第column列 每行title
 */
- (NSString *)menu:(DOPDropDownMenu *)menu titleForRowAtIndexPath:(DOPIndexPath *)indexPath {
    if (indexPath.column == 0) {
        // 返回学院名
        return self.collegeArray[indexPath.row];
    } else if (indexPath.column == 1) {
        // 返回活动类型名
        return self.teacherTypeArray[indexPath.row];
    }else{
        // 可预约状态
        return  self.stateArray[indexPath.row];
    }
}

/**
 *  点击代理，点击了第column 第row 或者item项，如果 item >=0
 */
-(void)menu:(DOPDropDownMenu *)menu didSelectRowAtIndexPath:(DOPIndexPath *)indexPath {
    if (indexPath.column == 0) {
        // 如果相等,就不需要赋值,不需要下拉刷新,直接返回
        if (self.college == indexPath.row) {
            return ;
        }
        // 学院
        self.college = indexPath.row;
    }else if (indexPath.column == 1) {
        // 如果相等,就不需要赋值,不需要下拉刷新,直接返回
        if (self.teacherType == indexPath.row) {
            return ;
        }
        // 导师类型
        self.teacherType = indexPath.row;
    } else if (indexPath.column == 2) {
        // 如果相等,就不需要赋值,不需要下拉刷新,直接返回
        if (self.state == indexPath.row) {
            return ;
        }
        // 可预约状态
        self.state = indexPath.row;
    }
    [self.tableView.mj_header beginRefreshing];

}

- (void)menu:(DOPDropDownMenu *)menu willShowAtColumn:(NSInteger)column{
    NSLog(@"willShowAtColumn:%d",column);

    // tableView不能再滚动
    self.tableView.scrollEnabled = false;
}

- (void)menu:(DOPDropDownMenu *)menu willDisappearAtColumn:(NSInteger)column {
    NSLog(@"willDisappearAtColumn:%d",column);

    // tableView可以再次滚动
    self.tableView.scrollEnabled = true;
}

#pragma mark - UITableView代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return self.menu.height;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return self.menu;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView cellHeightForIndexPath:indexPath model:self.dataArray[indexPath.row] keyPath:@"model" cellClass:[TeacherCell class] contentViewWidth:SCREEN_WIDTH];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];

    // 取出cell
    TeacherCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    // 弹出导师详情
    TeacherInfoViewController *teacherInfoViewController = [[TeacherInfoViewController alloc] init];
    teacherInfoViewController.teacherID = cell.model.tea_id;
    [self.navigationController pushViewController:teacherInfoViewController animated:true];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dataArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TeacherCell *teacherCell = [tableView dequeueReusableCellWithIdentifier:@"CELLID"];
    if (teacherCell == nil) {
        teacherCell = [[TeacherCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELLID"];
    
    }
    // 取出模型
    TeacherModel *model = self.dataArray[indexPath.row];
    
    // 初始化cell数据
    teacherCell.model = model;
    return teacherCell;
}



@end
